2019年7月11日
Jerry
7976
2020年4月8日
好久没有操作数据库,基本的增删改查都忘记了。每次要用的时候总是需要百度,浪费时间。这里写一个小Demo,下次直接copy!
一、Access数据库创建
直接右键创建数据库,打开后创建表 login_sheet,字段如下图按照要求指定,我这里有三个字段:用户名user,密码pass,角色role:
二、数据库连接:
打开VS--->工具--->连接数据库:选择Access数据库文件,选择我们创建的数据库文件。
测试连接OK后,点击 高级 ,可以看到一个Provider:
这个需要复制一下,我们之后的程序会用到:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Csharp\WindowsFormsApp1\WindowsFormsApp1\res\App1.mdb
三、读取数据(查)
private void button3_Click(object sender, EventArgs e)
{
oleDb = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Csharp\WindowsFormsApp1\WindowsFormsApp1\res\App1.mdb");
oleDb.Open();
listView1.Items.Clear();
string sql = "select * from login_sheet";
OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter(sql, oleDb); //创建适配对象
DataTable dt = new DataTable(); //新建表对象
dbDataAdapter.Fill(dt); //用适配对象填充表对象
foreach (DataRow item in dt.Rows)
{
Console.WriteLine(item[0] + " | " + item[1] + " | " + item[2]);
ListViewItem litem = new ListViewItem();
litem.Text = item[0].ToString();
litem.SubItems.Add(item[1].ToString());
litem.SubItems.Add(item[2].ToString());
litem.SubItems.Add(item[3].ToString());
listView1.BeginUpdate();
listView1.Items.Add(litem);
listView1.EndUpdate();
}
oleDb.Close();
}
登陆函数:
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请填写完整!", "提示");
return;
}
string sql = "select * from login_sheet where user='" + textBox1.Text + "'";
Console.WriteLine(sql);
OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter(sql, oleDb); //创建适配对象
DataTable dt = new DataTable(); //新建表对象
try
{
dbDataAdapter.Fill(dt); //用适配对象填充表对象
}
catch
{
}
DataRow item = dt.Rows[0];
if (item[2].ToString() == textBox2.Text)
{
if (radioButton1.Checked && item[3].ToString() == "0")
{
MessageBox.Show("登陆成功!", "提示");
return;
}
else if (radioButton2.Checked && item[3].ToString() == "1")
{
MessageBox.Show("登陆成功!", "提示");
return;
}
}
MessageBox.Show("用户名密码错误!", "提示");
return;
}
四、注册函数(增)
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("请填写完整!", "提示");
return;
}
if (textBox3.Text != textBox2.Text)
{
MessageBox.Show("密码不一致!", "提示");
return;
}
OleDbConnection oleDb=null;
OleDbCommand oleDbCommand;
int res = 0;
try
{
oleDb = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Csharp\WindowsFormsApp1\WindowsFormsApp1\res\App1.mdb");
oleDb.Open();
string sql = "insert into login_sheet ([user],[pass],[role]) values('" + textBox1.Text + "','" + textBox2.Text + "','1')";
Console.WriteLine(sql);
oleDbCommand = new OleDbCommand(sql, oleDb);
res = oleDbCommand.ExecuteNonQuery(); //返回被修改的数目
}
catch (Exception e1)
{
Console.WriteLine(e1.ToString());
}
Console.WriteLine(res);
if (res <= 0)
{
MessageBox.Show("用户名已经存在 注册失败!", "提示");
oleDb.Close();
return;
}
MessageBox.Show("注册成功!", "提示");
oleDb.Close();
this.Close();
}
四、修改函数(改)
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == ""
|| textBox3.Text == "" || textBox4.Text == "")
{
MessageBox.Show("请填写完整!", "提示");
return;
}
if (textBox3.Text != textBox4.Text)
{
MessageBox.Show("密码不一致!", "提示");
return;
}
OleDbConnection oleDb = null;
OleDbCommand oleDbCommand;
oleDb = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Csharp\WindowsFormsApp1\WindowsFormsApp1\res\App1.mdb");
oleDb.Open();
string sql = "select * from login_sheet where user='" + textBox1.Text + "'";
Console.WriteLine(sql);
OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter(sql, oleDb); //创建适配对象
DataTable dt = new DataTable(); //新建表对象
try
{
dbDataAdapter.Fill(dt); //用适配对象填充表对象
}
catch (Exception e1)
{
Console.WriteLine(e1.ToString());
}
DataRow item = dt.Rows[0];
if (item[2].ToString() == textBox2.Text)
{
int res = 0;
try
{
string sql2 = "update login_sheet set pass='" + textBox3.Text + "' where user='" + textBox1.Text + "'";
Console.WriteLine(sql2);
oleDbCommand = new OleDbCommand(sql2, oleDb);
res = oleDbCommand.ExecuteNonQuery(); //返回被修改的数目
}
catch (Exception e1)
{
Console.WriteLine(e1.ToString());
}
Console.WriteLine(res);
if (res > 0)
{
MessageBox.Show("修改成功!", "提示");
oleDb.Close();
this.Close();
return;
}
}
MessageBox.Show("用户名密码错误!", "提示");
return;
}
下次直接拿来用,毕竟一个资深程序员必须要会 ctrl+C ctrl+V !!!
原创文章,转载请注明出处:
https://jerrycoding.com/article/csharp-access-db
微信
支付宝